// ==UserScript==
// @name         多点条码打印
// @namespace    https://gitee.com/vbox007/Tampermonkey/raw/master/
// @version      0.16
// @description  我也不想回答那些奇怪的问题，所以别问我怎么做到的
// @author       vbox007
// @icon         
// @match        https://fulproduce-partner.dmall.com/produceOrderDetail/detail*
// @require      https://cdn.bootcdn.net/ajax/libs/jsbarcode/3.11.0/JsBarcode.all.min.js
// ==/UserScript==

(function () {
    'use strict';
    var czbc = function (wtd, powin, wpd, wwd) {
        var zbbarcode = '2';
        if (wtd.indexOf('生鲜计数') == -1) {
            zbbarcode += '6';
        } else {
            zbbarcode += '5';
        }
        zbbarcode += powin;
        var mwpd = wpd.replace('.', '');
        var t = '';
        for (i = 0; i < 5 - mwpd.length; i++) {
            t += '0';
        }
        zbbarcode += t + mwpd;
        if (zbbarcode[1] == '5') {
            zbbarcode += '00001';
        } else {
            var mwwd = wwd.replace('.', '');
            if (mwwd >>> 0 >= 1500) {
                mwwd = '14999';
            } else {
                mwwd += '0';
            };
            t = '';
            for (var i = 0; i < 5 - mwwd.length; i++) {
                t += '0';
            }
            zbbarcode += t + mwwd;
        }
        var s = 0;
        for (i = 0; i < 17; i++) {
            if (i % 2 == 0) {
                s += zbbarcode[i] * 3;
            } else {
                s += zbbarcode[i] * 1;
            }
        }
        s = s * 9 % 10;
        zbbarcode += s;
        return zbbarcode;
    };//createzbbarcode
    var bcobj = JSON.parse(document.body.innerText);
    document.head.innerHTML = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />\n<link rel="shortcut icon" type="image/ico" href="">\n<title>越秀多点条码打印页</title>\n<style>\ndiv {width:240px;margin: 0 auto;text-align:center;}\n.bc-center {width:240px;height:168px;margin: 0 auto;text-align:center;}\n</style>';
    document.body.innerHTML = '<div id="ddbarcode" class="bc-center"></div>';
    var innh = '<p><br/>' + bcobj.data.produce_order__order_id + '(' + (bcobj.data.wareList.length - 1) + '种' + bcobj.data.wareList[bcobj.data.wareList.length - 1].produce_order_ware__pick_num + '件)</p>';
    bcobj.data.wareList.sort(function (a, b) {
        return (a.produce_order_ware__matnr - b.produce_order_ware__matnr);
    }); //按拣货码排序一次
    for (var i = 3; i > 0; i--) {
        bcobj.data.wareList.sort(function (a, b) {
            if (a['produce_order_ware__category_name_l' + i] < b['produce_order_ware__category_name_l' + i]) {
                return 1;
            }
            if (a['produce_order_ware__category_name_l' + i] > b['produce_order_ware__category_name_l' + i]) {
                return -1;
            }
            return 0;
        });
    } //按照商品类别排序
    var ddbcdiv = document.getElementById('ddbarcode');
    var bcsvg = new Array();
    var cbarc = '123456123456123456';
    for (i = 0; i < bcobj.data.wareList.length; i++) {
        if (bcobj.data.wareList[i].produce_order_ware__item_num != null) {
            innh += '<p>';
            if (bcobj.data.wareList[i].produce_order_ware__item_num.length == 5 || bcobj.data.wareList[i].produce_order_ware__matnr.length == 5) {
                var powinm = bcobj.data.wareList[i].produce_order_ware__item_num.length == 5 ? bcobj.data.wareList[i].produce_order_ware__item_num : bcobj.data.wareList[i].produce_order_ware__matnr;
                innh += powinm;
                cbarc = (bcobj.data.wareList[i].produce_order_ware__matnr.length == 18 && bcobj.data.wareList[i].produce_order_ware__matnr.substring(0, 2)=='23') ? bcobj.data.wareList[i].produce_order_ware__matnr : czbc(
                    bcobj.data.wareList[i].wareTypeDesc,
                    powinm,
                    bcobj.data.wareList[i].warePriceDesc,
                    bcobj.data.wareList[i].wareWeightDesc
                );
                innh += bcobj.data.wareList[i].produce_order_ware__ware_name;
                if (bcobj.data.wareList[i].produce_order_ware__pick_num > 1) {
                    innh += '<span style="font-weight:bold;font-size:32px">×' + bcobj.data.wareList[i].produce_order_ware__pick_num + '</span>';
                }
                innh += '</p>';
                bcsvg[bcsvg.length] = {
                    'svgid': 'bc' + cbarc,
                    'barcode': cbarc
                };
                innh += '<svg id="bc' + cbarc + '"/>';
            } else {
                innh += bcobj.data.wareList[i].produce_order_ware__ware_name;
                if (bcobj.data.wareList[i].produce_order_ware__pick_num > 1) {
                    innh += '<span style="font-weight:bold;font-size:32px">×' + bcobj.data.wareList[i].produce_order_ware__pick_num + '</span>';
                }
                innh += '</p>';
                bcsvg[bcsvg.length] = {
                    'svgid': 'bc' + bcobj.data.wareList[i].produce_order_ware__item_num,
                    'barcode': bcobj.data.wareList[i].produce_order_ware__item_num
                };
                innh += '<svg id="bc' + bcobj.data.wareList[i].produce_order_ware__item_num + '"/>';
            }
        }
    }
    ddbcdiv.innerHTML = innh;
    for (i = 0; i < bcsvg.length; i++) {
        JsBarcode("#" + bcsvg[i].svgid, bcsvg[i].barcode, {
            format: "CODE128",
            margin: 0,
            width: 1.7,
            height: 48,
            displayValue: true,
            fontSize: 20
        });
    }
    window.print();
    // Your code here...
})();